# 模型，数据库相关的文件
# 模型 ==> 数据库
# 类   ==> 表这个结构
# 类的属性 ==> 表中的字段
# 单个对象 ==> 将要存入或者已经存入表中的一行数据
from .exts import db
from datetime import datetime


# 产品信息
class Product(db.Model):
    __tablename__ = 'Product'
    product_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    product_type = db.Column(db.String(30), nullable=False)
    product_type_id = db.Column(db.Integer, nullable=False)
    product_name = db.Column(db.String(30), nullable=False, unique=True)
    product_details = db.Column(db.Text, nullable=True)
    product_picture = db.Column(db.Text, nullable=True)
    product_music_address = db.Column(db.Text, nullable=True)
    product_video_address = db.Column(db.Text, nullable=True)
    product_price = db.Column(db.Text, nullable=True)
    product_evalute = db.Column(db.Text, nullable=True)

    def __init__(self, product_type, product_type_id, product_name, product_details, product_picture,
                 product_music_address, product_video_address, product_price, product_evalute):
        self.product_type = product_type
        self.product_type_id = product_type_id
        self.product_name = product_name
        self.product_details = product_details
        self.product_picture = product_picture
        self.product_music_address = product_music_address
        self.product_video_address = product_video_address
        self.product_price = product_price
        self.product_evalute = product_evalute


# 用户信息
class Users(db.Model):
    __tablename__ = 'users'
    user_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_name = db.Column(db.String(30), nullable=False)
    user_number = db.Column(db.String(30), nullable=False)
    user_password = db.Column(db.String(30), nullable=False)
    user_fan = db.Column(db.Integer, default=0)
    user_attention = db.Column(db.Integer, default=0)
    user_state = db.Column(db.Integer, default=0)
    user_introduce = db.Column(db.Text, nullable=True)
    user_fraction = db.Column(db.Integer, default=0)
    user_grade = db.Column(db.Integer, default=1)
    is_active = db.Column(db.Boolean, default=True)
    user_upgrade = db.Column(db.Integer, default=0)
    user_gender = db.Column(db.Text, default='未知')
    user_birthday = db.Column(db.Text, default='未知')
    user_city = db.Column(db.Text, default='未知')
    user_province = db.Column(db.Text, default='未知')

    def __init__(self, user_name, user_upgrade, user_gender,
                 user_number, user_password,
                 user_fan=None, user_attention=None,
                 user_state=None, user_introduce=None, user_fraction=None,
                 user_grade=None, is_active=True):
        self.user_name = user_name
        self.user_number = user_number
        self.user_password = user_password
        self.user_fan = user_fan
        self.user_attention = user_attention
        self.user_state = user_state
        self.user_introduce = user_introduce
        self.user_fraction = user_fraction
        self.user_grade = user_grade
        self.is_active = is_active
        self.user_upgrade = user_upgrade
        self.user_gender = user_gender

    def get_id(self):
        return self.user_id


# 管理员
class Manager(db.Model):
    __tablename__ = 'manager'
    user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    manager_id = db.Column(db.Integer, primary_key=True, autoincrement=True)

    def __init__(self, user_id):
        self.user_id = user_id


# 点赞
class Like(db.Model):
    __tablename__ = 'like'
    like_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    like_user = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    like_object = db.Column(db.Integer, nullable=False)
    like_type = db.Column(db.String(30), nullable=False)
    like_time = db.Column(db.TIMESTAMP, default=datetime.now())

    def __init__(self, like_user, like_object, like_type):
        self.like_user = like_user
        self.like_object = like_object
        self.like_type = like_type


# 关注和粉丝
class AttentionFan(db.Model):
    __tablename__ = 'attention_fan'
    af_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    af_parent = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    af_child = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    af_time = db.Column(db.TIMESTAMP, default=datetime.now())

    def __init__(self, af_parent, af_child):
        self.af_parent = af_parent
        self.af_child = af_child


# 用户黑名单
class Blacklist(db.Model):
    __tablename__ = 'blacklist'
    blacklist_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    blacklist_parent = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    blacklist_child = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    blacklist_time = db.Column(db.TIMESTAMP, default=datetime.now())

    def __init__(self, blacklist_parent, blacklist_child):
        self.blacklist_parent = blacklist_parent
        self.blacklist_child = blacklist_child


# 用户收藏夹
class Favoritebag(db.Model):
    __tablename__ = 'favoritebag'
    favoritebag_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    favoritebag_user = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    favoritebag_name = db.Column(db.String(30), nullable=False)
    favoritebag_time = db.Column(db.TIMESTAMP, default=datetime.now())

    def __init__(self, favoritebag_user, favoritebag_name):
        self.favoritebag_user = favoritebag_user
        self.favoritebag_name = favoritebag_name


# 用户收藏
class Favorite(db.Model):
    __tablename__ = 'favorite'
    favorite_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    favorite_user = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    favorite_time = db.Column(db.TIMESTAMP, default=datetime.now())
    favorite_object = db.Column(db.Integer, nullable=False)
    favorite_type = db.Column(db.String(30), nullable=False)

    def __init__(self, favorite_user, favorite_object, favorite_type):
        self.favorite_user = favorite_user
        self.favorite_object = favorite_object
        self.favorite_type = favorite_type


# 用户收藏关系
class FavoriteRelation(db.Model):
    __tablename__ = 'favorite_relation'
    relation_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    relation_favorite_bag_id = db.Column(db.Integer, db.ForeignKey('favoritebag.favoritebag_id'))
    relation_favorite_id = db.Column(db.Integer, db.ForeignKey('favorite.favorite_id'))
    relation_favorite_user = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    relation_favorite_time = db.Column(db.TIMESTAMP, default=datetime.now())

    def __init__(self, relation_favorite_bag_id, relation_favorite_id, relation_favorite_user):
        self.relation_favorite_bag_id = relation_favorite_bag_id
        self.relation_favorite_id = relation_favorite_id
        self.relation_favorite_user = relation_favorite_user


# 用户打卡
class Card(db.Model):
    __tablename__ = 'card'
    card_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    card_user = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    card_time = db.Column(db.TIMESTAMP, default=datetime.now())

    def __init__(self, card_user):
        self.card_user = card_user


# 问答模块
class Questions(db.Model):
    __tablename__ = 'questions'
    question_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    question_user = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    question_text = db.Column(db.Text, nullable=False)
    question_title = db.Column(db.String(30), nullable=False)
    question_time = db.Column(db.TIMESTAMP, default=datetime.now())
    question_integral = db.Column(db.Integer, nullable=False)
    question_answer = db.Column(db.Integer, nullable=True, default=0)
    question_see = db.Column(db.Integer, default=0)

    def __init__(self, question_user, question_text, question_title, question_integral,
                 question_answer):
        self.question_user = question_user
        self.question_text = question_text
        self.question_title = question_title
        self.question_integral = question_integral
        self.question_answer = question_answer


# 问题评论关系表
class QuestionRelation(db.Model):
    __tablename__ = 'question_relation'
    querelation_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    querelation_object = db.Column(db.Integer, db.ForeignKey('questions.question_id'))
    querelation_review = db.Column(db.Integer, db.ForeignKey('review.review_id'))
    querelation_number = db.Column(db.Integer, default=1)
    querelation_time = db.Column(db.TIMESTAMP, default=datetime.now())

    def __init__(self, querelation_id, querelation_object, querelation_review, querelation_number):
        self.querelation_id = querelation_id
        self.querelation_object = querelation_object
        self.querelation_review = querelation_review
        self.querelation_number = querelation_number


# 专栏模块
class Special(db.Model):
    __tablename__ = 'special'
    special_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    special_user = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    special_title = db.Column(db.String(30), nullable=False)
    special_time = db.Column(db.TIMESTAMP, default=datetime.now())
    special_see = db.Column(db.Integer, default=0)

    def __init__(self, special_user, special_title):
        self.special_user = special_user
        self.special_title = special_title


# 专栏底下的动态
class SpDy(db.Model):
    __tablename__ = 'spdy'
    spdy_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    spdy_user = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    spdy_title = db.Column(db.String(30), nullable=False)
    spdy_time = db.Column(db.TIMESTAMP, default=datetime.now())
    spdy_text = db.Column(db.Text, nullable=False)
    spdy_father = db.Column(db.Integer, db.ForeignKey('special.special_id'))
    spdy_see = db.Column(db.Integer, default=0)

    def __init__(self, spdy_user, spdy_title, spdy_text, spdy_father):
        self.spdy_user = spdy_user
        self.spdy_title = spdy_title
        self.spdy_text = spdy_text
        self.spdy_father = spdy_father


# 标签表
class Tag(db.Model):
    __tablename__ = 'tag'
    tag_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    tag_name = db.Column(db.String(30), nullable=False)
    tag_time = db.Column(db.TIMESTAMP, default=datetime.now())

    def __init__(self, tag_name):
        self.tag_name = tag_name


# 标签关联表
class TagRelation(db.Model):
    __tablename__ = 'tag_relation'
    tagrelation_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    tagrelation_type = db.Column(db.String(30), nullable=False)
    tagrelation_tag_id = db.Column(db.Integer, db.ForeignKey('tag.tag_id'))
    tagrelation_object = db.Column(db.Integer, nullable=False)

    def __init__(self, tagrelation_type, tagrelation_tag_id, tagrelation_object):
        self.tagrelation_type = tagrelation_type
        self.tagrelation_tag_id = tagrelation_tag_id
        self.tagrelation_object = tagrelation_object


# 图片表
class Picture(db.Model):
    __tablename__ = 'picture'
    picture_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    picture_ob_type = db.Column(db.String(30), nullable=False)
    picture_object = db.Column(db.Integer, nullable=False)
    picture_address = db.Column(db.Text, nullable=False)
    picture_size = db.Column(db.Integer, nullable=True)
    picture_type = db.Column(db.String(30), nullable=True)
    picture_time = db.Column(db.TIMESTAMP, default=datetime.now())

    def __init__(self, picture_ob_type, picture_object, picture_address, picture_size=None,
                 picture_type=None):
        self.picture_ob_type = picture_ob_type
        self.picture_object = picture_object
        self.picture_address = picture_address
        self.picture_size = picture_size
        self.picture_type = picture_type


# 用户动态
class Dynamic(db.Model):
    __tablename__ = 'dynamic'
    dynamic_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    dynamic_user = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    dynamic_text = db.Column(db.Text, nullable=False)
    dynamic_title = db.Column(db.String(30), nullable=False)
    dynamic_time = db.Column(db.TIMESTAMP, default=datetime.now())
    dynamic_see = db.Column(db.Integer, default=0)

    def __init__(self, dynamic_user, dynamic_text, dynamic_title):
        self.dynamic_user = dynamic_user
        self.dynamic_text = dynamic_text
        self.dynamic_title = dynamic_title


# 用户书架
class Bookshelf(db.Model):
    __tablename__ = 'bookshelf'
    bookshelf_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    bookshelf_user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    bookshelf_title = db.Column(db.Text, nullable=False)
    bookshelf_time = db.Column(db.TIMESTAMP, default=datetime.now())
    bookshelf_see = db.Column(db.Integer, default=0)

    def __init__(self, bookshelf_user_id, bookshelf_title):
        self.bookshelf_user_id = bookshelf_user_id
        self.bookshelf_title = bookshelf_title


# 用户文章关系表
class Bookrelation(db.Model):
    __tablename__ = 'bookrelation'
    bookrelation_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    bookrelation_bookshelf_id = db.Column(db.Integer, db.ForeignKey('bookshelf.bookshelf_id'))
    bookrelation_dynamic_id = db.Column(db.Integer, db.ForeignKey('dynamic.dynamic_id'))
    bookrelation_user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    bookrelation_time = db.Column(db.TIMESTAMP, default=datetime.now())

    def __init__(self, bookrelation_bookshelf_id, bookrelation_dynamic_id, bookrelation_user_id):
        self.bookrelation_bookshelf_id = bookrelation_bookshelf_id
        self.bookrelation_dynamic_id = bookrelation_dynamic_id
        self.bookrelation_user_id = bookrelation_user_id


# 评论区闭包
class Review(db.Model):
    __tablename__ = 'review'
    review_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    review_object_type = db.Column(db.String(30), nullable=False)
    review_user = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    review_object_id = db.Column(db.Integer, nullable=False)
    review_grade_top = db.Column(db.Integer, nullable=True)
    review_top_id = db.Column(db.Integer, nullable=True)
    review_backer = db.Column(db.Integer, nullable=True)
    review_text = db.Column(db.Text, nullable=False)
    review_time = db.Column(db.TIMESTAMP, default=datetime.now())

    def __init__(self, review_user, review_text, review_grade_top,
                 review_top_id, review_backer, review_object_type,
                 review_object_id):
        self.review_user = review_user
        self.review_text = review_text
        self.review_grade_top = review_grade_top
        self.review_top_id = review_top_id
        self.review_backer = review_backer
        self.review_object_type = review_object_type
        self.review_object_id = review_object_id


class Discord(db.Model):
    __tablename__ = 'discord'
    discord_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    discord_user = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    discord_reception = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    discord_text = db.Column(db.Text, nullable=False)
    discord_picture = db.Column(db.Text, nullable=True)
    discord_time = db.Column(db.TIMESTAMP, default=datetime.now())
    discord_state = db.Column(db.Boolean, default=True)

    def __init__(self, discord_user, discord_reception,
                 discord_text, discord_picture, discord_state):
        self.discord_user = discord_user
        self.discord_reception = discord_reception
        self.discord_text = discord_text
        self.discord_picture = discord_picture
        self.discord_state = discord_state


# 用户历史记录
class History(db.Model):
    __tablename__ = 'history'
    history_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    history_user = db.Column(db.Integer, db.ForeignKey('users.user_id'))
    history_text = db.Column(db.Text, nullable=False)
    history_time = db.Column(db.TIMESTAMP, default=datetime.now())

    def __init__(self, history_user, history_text):
        self.history_user = history_user
        self.history_text = history_text

# 模型Model： 类 hello world!!!
# 必须继承 db.Model
# db.Column: 表示字段
# db.Integer: 表示整数
# primary_key= True : 主键
# db.ForeignKey('表的名称.元素名称') : 设置成外键
# autoincrement = True ：自动递增
# db.String(30) ： 可变字符串长度为30
# unique= True ： 字符串唯一约束
# index = True ： 创建一个普通索引
# default=1 ；默认值为1
# sex = db.Column(db.Boolean,
#                 default= True)
# salary = db.Column(db.Float,
#                    default=100000,
#                    nullable=False)
# #nullable=False : 是否允许为空
